// utils/websocket/handlers/group.js

import { MESSAGE_TYPES } from '../messageTypes';

/**
 * 处理群聊消息
 */
export default function handleGroupMessage(message) {
  console.log('收到群聊消息:', message);

  const content = message.message;
  const userIdMatch = /用户id(\d+)/.exec(content);
  const groupIdMatch = /群id(\d+)/.exec(content);

  const userId = userIdMatch ? parseInt(userIdMatch[1], 10) : null;
  const groupId = groupIdMatch ? parseInt(groupIdMatch[1], 10) : null;

  if (!userId || !groupId) {
    uni.showToast({ title: '消息格式错误', icon: 'none' });
    return;
  }

  // 广播给页面监听
  uni.$emit('websocket-group', {
    type: MESSAGE_TYPES.GROUP,
    userId,
    groupId,
    raw: message
  });

  uni.showToast({
    title: `群 ${groupId} 收到新消息`,
    icon: 'none',
    duration: 3000
  });
}